<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 下午 3:57
 */
namespace app\member\model;
use think\console\command\make\Model;
use think\Db;

class Member extends Model{
    #添加用户，注册
    public function mem_add($data){
        $add = Db::name('member')->insertGetId($data);
        return $add;
    }
    #登录验证
    public function login($data){
        $info = Db::name('member')
            ->where(['username'=>$data['username'],'del'=>1])
            ->find();
        return $info;
    }
    #插入登录日志
    public function login_log($data){
        $log = Db::name('member_log')
            ->insertGetId($data);
        return $log;
    }
    #获取用户信息
    public function mem_info($data){
//        $field = ''
        $info = Db::name('member')->where(['id'=>$data['id']])->find();
        return $info;
    }
    #修改用户信息
    public function mem_update($data){
        $id = $data['id'];
        unset($data['id']);
        if (isset($data['img'])){
            $img = Db::name('member')->where(['id'=>$id])->value('img');
            $url=$_SERVER["DOCUMENT_ROOT"]."/".$img;
            $old_url='D:/phpStudy/WWW/blog/public/static/images/timg.jpg';
            if (file_exists($url) && $url!=$old_url){
                unlink($url);
            }
        }
        $up = Db::name('member')
            ->where(['id'=>$id])->update($data);
        if ($up>0){
            $res = Db::name('member')->where(['id'=>$id,'del'=>1])->find();
            return ['code'=>200,'msg'=>config('msg.200'),'data'=>$res];
        }else{
            return ['code'=>10025,'msg'=>config('msg.10025')];#数据未更新
        }
    }
    #用户列表
    public function mem_list($data){
        #条件
        $where = 'a.del=1';
        $where.= isset($data['keyword'])? ' and CONCAT(username) like "%'.$data['keyword'].'%"':'';
        $where.= isset($data['identity'])? ' and identity='.$data['identity']:'';
        #总条数
        $total = Db::name('member')->alias('a')
            ->join('role b','b.role_id=a.identity')->where($where)->count();
        #总页数
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }
        #列表
        $field = 'a.id,a.username,a.email,a.phone,a.introduce,a.img,a.createtime,b.role_name';
        $list = Db::name('member')
            ->alias('a')
            ->join('role b','b.role_id=a.identity')
            ->field($field)
            ->where($where)->page($data['curr'],$data['limits'])->select();

        #返回值
        $result = [
            'list'  =>$list,
            'pages'  =>$page,
            'limits'=>$data['limits'],
            'total' =>$total,
            'curr'  =>$data['curr']
        ];
        return $result;
    }
    #删除用户
    public function del($data){
        $del = Db::name('member')->where(['id'=>['in',$data['id']],'del'=>1])->update(['del'=>0]);
        return $del;
    }
    #批量导入
    public function user_import($data){
        $import_info = Db::name('member')->insertGetId($data);
        return $import_info;
    }
    #批量导出
    public function user_export($data){
        #判断关键字、学段、学科、年级、出版社的筛选或搜索
        $keyword = isset($data['keyword'])?trim($data['keyword']):'';
        $identity = isset($data['identity'])?intval($data['identity']):'';

        #搜索条件
        $where = 'a.del=1';
        $where.= $keyword? ' and CONCAT(username) like "%'.$keyword.'%"':'';
        $where.= $identity? ' and CONCAT(identity) like "%'.$identity.'%"':'';

        #需要返回的字段
        $field = 'a.id,a.username,a.email,a.phone,a.introduce,a.img,b.role_name';
        $list = Db::name('member')
            ->alias('a')->field($field)
            ->join('role b','b.role_id=a.identity')
            ->where($where)->select();

        #返回结果
        return ['code'=>200,'msg'=>config('msg.200'),'data'=>$list];#表内容
    }

}